//
//  ZYSecondViewController.m
//  MusicMV
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 Eternity. All rights reserved.
//

#import "ZYSecondViewController.h"

@interface ZYSecondViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tempArr;
@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, assign) NSInteger number;
@end

@implementation ZYSecondViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _number = 1;
//        [self create];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self create];
        
    }];
    [self.tableView.mj_header beginRefreshing];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ZYSecondTableViewCell class] forCellReuseIdentifier:@"ZYSecondTableViewCell"];
    
    
    self.tableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        _number++;
        [self create];
    }];
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return HEIGHT / 6 + 20;
    
}

#pragma mark - item个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _tempArr.count;
    
}
#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYThirstViewController *vc = [[ZYThirstViewController alloc] init];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:vc animated:YES completion:^{
        
    }];
    
    
}



#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYSecondTableViewCell"];
    ZYSecondModel *model = _tempArr[indexPath.row];
    cell.model = model;
    return cell;
    
}

- (void)create {
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=iPhone&key=%@&pageId=%ld&pageSize=20", _model.key, _number];
    
   [XRAFNetworkTool GETUrl:str body:nil headFile:nil response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
       NSArray *arr = responseObject[@"list"];
       
       self.tempArr = [ZYSecondModel modelWithArray:arr];
       [_tableView reloadData];
       [_tableView.mj_header endRefreshing];
   } failure:^(NSURLSessionDataTask *task, NSError *error) {
       
   }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
